define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    $.widget('rk.pageNoticeBtnCtrl', {
        _create: function() {
            var me = this;
            var elem = me.element;
            elem.click(function() {
                me.open();
            });
        },
        _init: function() {
            var me = this;
            var elem = me.element;
        },
        currentNum: 0,
        getNum: function() {
            var me = this;
            return me.currentNum;
        },
        setNum: function(num) {
            var me = this;
            var elem = me.element;
            var tip = elem.find('.announcement-new-tip');
            me.currentNum = num;
            if (num == 0 || (!$.isNumeric(num) && num != "99+")) {
                tip.hide();
            } else {
                tip.show();
                var txt = (num > 99 ? '99+' : num);
                tip.html(txt);
            }
        },
        open: function(ctrlOpt, callback) {
            if (typeof ctrlOpt == 'undefined') ctrlOpt = {};
            if (typeof callback == 'undefined') callback = {};
            var me = this;
            var elem = me.element;
            //if(window._hmt){_hmt.push(['_trackEvent', 'operate', 'notice', window.SESSION.tenantName]);}
            require.async('oa/js/notice/notiPanelCtrl', function() {
                rk.oa.popupPanel({
                        onSuccess: function(panelSel){
                            var panel = $(panelSel);
                            panel.notiPanelCtrl(ctrlOpt);
                            if($.isFunction(callback))(callback)(panelSel);
                        },
                        onHide: function(){
                            rk.post("/json/oa_notice/unRead-counts.action?a=" + Math.random(), {}, function (json) {
                                if (json.status === 0) {
                                    var data = json.unRead_counts;
                                    var nums = data.remindUnreadCount + data.noticeUnreadCount + data.atmeUnreadCount + data.annoUnreadCount;
                                    if (nums - 0 > 99) {
                                        nums = "99+";
                                    }
                                    me.setNum(nums)
                                } else {
                                    me.setNum('setNum', 0);
                                }
                            });
                        }
                });
            });
        }
    });
});